package com.qdxwx.data.service.manager;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.DateUtils;
import com.qdxwx.data.mapper.manager.ManagerOnlineMapper;
import com.qdxwx.models.manager.ManagerOnline;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 在线用户 服务层处理
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class ManagerOnlineServiceImpl implements ManagerOnlineService
{
    @Autowired
    private ManagerOnlineMapper managerOnlineMapper;

    @Override
    public ManagerOnline getById(String sessionId)
    {
        return managerOnlineMapper.selectOnlineById(sessionId);
    }

    @Override
    public void deleteById(String sessionId)
    {
        ManagerOnline userOnline = getById(sessionId);
        if ((null != userOnline))
        {
            managerOnlineMapper.deleteOnlineById(sessionId);
        }
    }

    @Override
    public void batchDelete(List<String> sessions)
    {
        for (String sessionId : sessions)
        {
            ManagerOnline userOnline = getById(sessionId);
            if ((null != userOnline))
            {
                managerOnlineMapper.deleteOnlineById(sessionId);
            }
        }
    }

    @Override
    public void save(ManagerOnline online) {
        //先删除，再保存
        managerOnlineMapper.deleteOnlineById(online.getSessionId());
        managerOnlineMapper.saveOnline(online);
    }

    @Override
    public List<ManagerOnline> getList(ManagerOnline userOnline, PageRequest page)
    {
        return managerOnlineMapper.selectList(userOnline,page);
    }

    @Override
    public long count(ManagerOnline userOnline)
    {
        return managerOnlineMapper.selectCount(userOnline);
    }

    @Override
    public void forceLogout(String sessionId)
    {
        managerOnlineMapper.deleteOnlineById(sessionId);
    }

    @Override
    public List<ManagerOnline> getListByExpired(LocalDateTime expiredDate)
    {
        String lastAccessTime = DateUtils.toString(expiredDate, "yyyy-MM-dd HH:mm:ss");
        return managerOnlineMapper.selectOnlineByExpired(lastAccessTime);
    }
}
